package com.example.chasshelper.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chasshelper.ClassroomStuActivity;
import com.example.chasshelper.LoginActivity;
import com.example.chasshelper.R;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;

public class ClassroomStuAdapter extends RecyclerView.Adapter<ClassroomStuAdapter.ClassroomStuViewHolder>{
    private Context mContext;
    //数据
    private List<Classroom> list;
    private Student student;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();

    private int mPosition;
    public int getmPosition() {
        return mPosition;
    }

    //接收数据
    public ClassroomStuAdapter(Context mContext, List<Classroom> list, Student student) {
        this.mContext = mContext;
        this.list = list;
        this.student = student;
        System.out.println("所有课堂信息"+list);
        System.out.println("学生为："+student);
    }

    /*
    * 绑定布局
    * */
    @NonNull
    @Override
    public ClassroomStuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ClassroomStuViewHolder classroomStuViewHolder = new ClassroomStuViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_classroom_rv_stu, parent, false));
        return classroomStuViewHolder;
    }

    /*
    * 设置每一个item
    * */
    @Override
    public void onBindViewHolder(@NonNull ClassroomStuViewHolder holder, int position) {
        //回显
        Classroom classroom = list.get(position);
       holder.tv_id.setText("课堂id:"+String.valueOf(classroom.getClassroomId()));
        holder.tv_name.setText("课堂名:"+classroom.getClassroomName());
        holder.tv_time.setText("创建时间:"+classroom.getClassroomTime());
        holder.tv_teacher.setText("授课教师:"+classroomDao.findTeacher(classroom).getTeacherName());

        //点击事件
        holder.ll_oneClassroom.setOnClickListener(new View.OnClickListener() {//进入课堂，并传递数据
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("student", student);
                intent.putExtra("classroom",classroom);
                intent.setClass(mContext, ClassroomStuActivity.class);
                mContext.startActivity(intent);
            }
        });

        //长按事件
        // 删除
        holder.ll_oneClassroom.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                mPosition = position;
                Toast.makeText(mContext,String.valueOf(mPosition),Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    /*
    * 返回item数量
    * */
    @Override
    public int getItemCount() {
        return list.size();
    }

    /*
    * item布局
    * */
    class ClassroomStuViewHolder extends RecyclerView.ViewHolder{
        //控件
        LinearLayout ll_oneClassroom;
        TextView tv_id;
        TextView tv_name;
        TextView tv_time;
        TextView tv_teacher;
        public ClassroomStuViewHolder(@NonNull View itemView) {
            super(itemView);
            //绑定控件
            ll_oneClassroom = itemView.findViewById(R.id.ll_oneClassroom);
            tv_id = itemView.findViewById(R.id.tv_id);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_teacher = itemView.findViewById(R.id.tv_teacher);
            itemView.setLongClickable(true);
        }
    }
}
